package com.uxwine.cmm;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

public abstract class UXSettings {

	protected SharedPreferences spf;
	
	protected Context mCtx;
	private String mName;
	
	public UXSettings(Context ctx, String name)
	{
		this(ctx, name, 0);
	}
	
	public UXSettings(Context ctx, String name, int mode)
	{
		mCtx = ctx.getApplicationContext();
		mName = name;
		if(null == mName)
			spf = PreferenceManager.getDefaultSharedPreferences(mCtx);
		else
			spf = mCtx.getSharedPreferences(mName, mode);		
	}
	
	public void set(String key, String value)
	{
		editor().putString(key, value).commit();
	}
	
	public void set(String key, int value)
	{
		editor().putInt(key, value).commit();
	}
	
	public void set(String key, long value)
	{
		editor().putLong(key, value).commit();
	}
	
	public void set(String key, boolean value)
	{
		editor().putBoolean(key, value).commit();
	}
	
	public int getInt(String key, int defValue)
	{
		return spf.getInt(key, defValue);
	}
	
	public String getString(String key, String defValue)
	{
		return spf.getString(key, defValue);
	}
	
	public long getLong(String key, long defValue)
	{
		return spf.getLong(key, defValue);
	}
	
	public boolean getBoolean(String key, boolean defValue)
	{
		return spf.getBoolean(key, defValue);
	}
	
	public SharedPreferences.Editor editor()
	{
		return spf.edit();
	}
	
	public void setOnChangedListener(OnSharedPreferenceChangeListener l)
	{
		spf.registerOnSharedPreferenceChangeListener(l);
	}
}
